//引入依赖文件
var express = require('express')
var app = express();
var db = require('./conn')
// var data={id:1,title:''}
// var router=require('./router/index')
// app.use('/li',router)
app.use((req, res, next) => {
    // 设置是否运行客户端设置 withCredentials
    // 即在不同域名下发出的请求也可以携带 cookie
    res.header("Access-Control-Allow-Credentials",true)
    // 第二个参数表示允许跨域的域名，* 代表所有域名  
    res.header('Access-Control-Allow-Origin',req.headers.origin) 
    res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS') // 允许的 http 请求的方法
    // // 允许前台获得的除 Cache-Control、Content-Language、Content-Type、Expires、Last-Modified、Pragma 这几张基本响应头之外的响应头
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With')
    // if (req.method == 'OPTIONS') {
    //     res.sendStatus(200)
    // } else {
    //     next()
    // }
    next()
})
//允许使用服务器端静态路径,app.use是调用中间件
// 解析 application/json
app.use(express.json())
// 解析 application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: false }));
app.use(express.static('./'))
app.post('/showFruits',(req,res)=>{
    var sql=`select * from fruit`
    db.query(sql,[],(data,fields)=>{//fields是形式参数可以随便取
        if(data){
            res.send({
                success:true,
                data,
                msg:'成功'
            })
        }else{
            res.send({
                success:false,
                msg:'失败'
            })
        }
    })
})
app.post('/delFruit',(req,res)=>{
    var sql=`delete from fruit where id=${req.body.id}`
    db.query(sql, [], (list, resolve) => {
        if (list) {
            res.send({
                success: true,
                msg: '成功',
            })
        } else {
            res.send({
                success: false,
                msg: '失败'
            })
        }
    })
})
app.post("/addFruit",(req,res)=>{
    var fruit=req.body.fruit
    var sql=`insert into fruit (fruitName,fruitQuantity,fruitPrice) values 
    ("${fruit.fruitName}",${fruit.fruitQuantity},${fruit.fruitPrice})`
    db.query(sql, [], (list, resolve) => {
        if (list) {
            res.send({
                success: true,
                msg: '成功',
            })
        } else {
            res.send({
                success: false,
                msg: '失败'
            })
        }
    })
})
app.post("/updateFruit",(req,res)=>{
    var fruit=req.body.fruit
    var sql=`update fruit set fruitName="${fruit.fruitName}",fruitQuantity=${fruit.fruitQuantity},
    fruitPrice=${fruit.fruitPrice} where id=${fruit.id}`
    db.query(sql, [], (list, resolve) => {
        if (list) {
            res.send({
                success: true,
                msg: '成功',
            })
        } else {
            res.send({
                success: false,
                msg: '失败'
            })
        }
    })
})                        
app.post('/h1/goods/list', (req, res) => {
    var sql = 'select * from t_goods'
    db.query(sql, [], (list, resolve) => {
        if (list) {
            res.send({
                success: true,
                msg: '成功',
                list
            })
        } else {
            res.send({
                success: false,
                msg: '失败'
            })
        }
    })
})
app.post('/h3/goods/list', (req, res) => {
    var sql = "select id,title from t_goods"
    db.query(sql, [], (result, fields) => {
        if (result) {
            res.send({
                success: true,
                msg: '成功',
                list: result
            })
        } else {
            res.send({
                success: false,
                msg: '失败'
            })
        }
    })
})
app.post('/login', (req, res) => {
    //post过来的数据，如果使用data属性时，需要用req.body获取值
    console.log(req.body)
    res.send(req.query)
})
app.get('/data', (req, res) => {
    var sql = 'select * from user'
    db.query(sql, [], (result, resolve) => {
        res.send(result)
    })
})
app.post('/h4/goods/add', (req, res) => {
    var sql = `insert into t_goods (title,content,imagePath,price) 
    values ('${req.query.inputTitle}','${req.query.inputContent}','${req.query.inputImage}',${req.query.inputPrice})`
    db.query(sql, [], (result, fields) => {
        if (result) {
            res.send({
                success: true,
                msg: '成功'
            })
        } else {
            res.send({
                success: false,
                msg: '失败'
            })
        }
    })
})
app.post('/h2/goods/findone/:id', (req, res) => {
    var sql = 'select * from t_goods where id=' + req.params.id
    db.query(sql, [], (result, fields) => {
        if (result) {
            res.send({
                success: true,
                msg: '成功',
                data: result
            })
        } else {
            res.send({
                success: false,
                msg: '失败'
            })
        }
    })
})
app.post('/insert', (req, res) => {
    var sql = `insert into fruits (name,quantity,price) values ('${req.body.name}'
    ,${req.body.quantity},${req.body.price})`
    db.query(sql, [], (result, resolve) => {
        res.send(result)
    })
})
app.post('/h1/goods/list', (req, res) => {
    var sql = 'select * from t_goods'
    db.query(sql, [], (result, resolve) => {
        if (result) {
            res.send({
                list: result,
                msg: '成功',
                success: true
            })
        } else {
            res.send({
                success: false,
                msg: '失败'
            })
        }
    })
})
app.delete('/delete', (req, res) => {
    var sql = `delete from fruits where ID=${req.query.id}`
    db.query(sql, [], (result, resolve) => {
        res.send(result)
    })
})
app.post('/h5/goods/findone/:id', (req, res) => {
    var sql = `select * from t_goods where id=${req.params.id}`
    db.query(sql, [], (result, fields) => {
        if (result) {
            res.send({
                success: true,
                msg: '成功',
                data: result
            })
        } else {
            res.send({
                success: false,
                msg: '失败'
            })
        }
    })
})
app.get('/listme',(req,res)=>{
    var sql='select * from data'
    db.query(sql, [], (result, resolve) => {
        res.send(result)
    })
})
app.post('/h5/goods/update',(req,res)=>{
    var sql=`UPDATE t_goods
    SET title='${req.query.inputTitle}',content='${req.query.inputContent}',
    imagePath='${req.query.inputImagePath}',price=${req.query.inputPrice}
    WHERE id=${req.query.inputId}`
    db.query(sql,[],(result,fields)=>{
        if(result){
            res.send({
                success:true,
                msg:'成功'
            })
        }else{
            res.send({
                success:false,
                msg:'失败'
            })
        }
    })
})
app.post('/h7/goods/list',(req,res)=>{
    var sql='select id,title from t_goods';
    db.query(sql,[],(result,fields)=>{
        if(result){
            res.send({
                success:true,
                msg:'成功',
                list:result
            })
        }else{
            res.send({
                success:false,
                msg:'失败'
            })
        }
    })
})
app.delete('/h7/goods/delete/:id',(req,res)=>{
    // console.log(req.params.id)
    var sql='DELETE FROM t_goods  WHERE id='+req.params.id
    db.query(sql,[],(result,fields)=>{
        if(result){
            res.send({
                "success": true,
                "msg":"成功"
            })
        }else{
            res.send({
                "success": false,
                "msg":"失败"
            })
        }
    })
})
app.post('/h8/travel/list',(req,res)=>{
    var sql = 'select * from t_travel_destination'
    db.query(sql, [], (list, resolve) => {
        if (list) {
            res.send({
                success: true,
                msg: '成功',
                list
            })
        } else {
            res.send({
                success: false,
                msg: '失败'
            }) 
        }
    })
})
app.post('/h9/travel/getMember',(req,res)=>{
    var sql=`select id from t_member where memberPhone="${req.query.phone}" and passWord="${req.query.password}"`
    db.query(sql,[],(result,fields)=>{
        if(result.length){
            console.log(result[0])
            res.send({
                success: true,
                msg: '成功'
            })
        }else{
            res.send({
                success: false,
                msg: '失败'
            })
        }
    })
})
app.listen(3000, () => {
    // 3000是监听的端口号,JSP中经常使用8080端口，默认是80端口
    console.log('服务器已经启动！')
})